using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SampleUHFAt900;
using System.Reflection;
using System.Windows.Forms;

namespace PVID_UHF_CE_Application
{
    public static class AppLogWriter
    {
        static string Debug = Convert.ToString(MobileConfiguration.Settings["Debug"]);
        static string LogFilePath = string.Empty;
        static AppLogWriter()
        {
            StreamWriter sw = null;
            try
            {
                if (Debug.Equals("True"))
                {

                    string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                    appPath = appPath.Remove(0, 6);
                    //MessageBox.Show(appPath);
                    if (!System.IO.Directory.Exists(Path.Combine(appPath, "\\Log\\").ToString()))
                    {
                        System.IO.Directory.CreateDirectory(Path.Combine(appPath, "\\Log\\"));
                        MessageBox.Show("Log Directory Created.");
                    }
                    //else
                    //    MessageBox.Show("Directory Exists." + Path.Combine(appPath, "\\Log\\").ToString());
                    string _LogFilePath = Path.Combine(appPath, "\\Log\\Log_" + DateTime.Now.ToString("dd-MM-yyyy") + ".txt");
                    if (!File.Exists(_LogFilePath))
                    {
                        sw = File.CreateText(_LogFilePath);
                        sw.WriteLine("File Created at: " + DateTime.Now.ToShortTimeString());
                    }
                    LogFilePath = _LogFilePath;
                }
                //MessageBox.Show("Class Instance Created");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally 
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                }
                //MessageBox.Show("Stream Writer Closed");
            }
        }

        public static void WriteLogEntry(string LogMessage)
        {
            try
            {
                UnicodeEncoding uc = new UnicodeEncoding();
                //string _LogContent = string.Format("{0}\t{1}.", DateTime.Now.ToShortTimeString(), LogMessage);
                //fs.Write(uc.GetBytes(_LogContent), 0, uc.GetByteCount(_LogContent));
                //MessageBox.Show("Before SW. LF Path: " + LogFilePath);
                StreamWriter sw = System.IO.File.AppendText(LogFilePath);
                //MessageBox.Show("After SW.");
                sw.WriteLine(string.Format(">>I:{0}<<", LogMessage));
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("LW: " + ex.Message);
            }
        }
        public static void WriteErrorLog(string LogMessage)
        {
            try
            {
                UnicodeEncoding uc = new UnicodeEncoding();
                string _LogContent = string.Format("{0}\t{1}.", DateTime.Now.ToShortTimeString(), LogMessage);
                //fs.Write(uc.GetBytes(_LogContent), 0, uc.GetByteCount(_LogContent));
                StreamWriter sw = System.IO.File.AppendText(LogFilePath);
                sw.WriteLine(string.Format(">>E:{0}<<", LogMessage));
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("LW: " + ex.Message);
            }
        }
        public static void WriteLog(string LogMessage)
        {
            try
            {
                UnicodeEncoding uc = new UnicodeEncoding();
                string _LogContent = string.Format("{0}\t{1}.", DateTime.Now.ToShortTimeString(), LogMessage);
                //fs.Write(uc.GetBytes(_LogContent), 0, uc.GetByteCount(_LogContent));
                StreamWriter sw = System.IO.File.AppendText(LogFilePath);
                sw.WriteLine(string.Format(">>M:{0}<<", LogMessage));
                sw.Flush();
                sw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("LW: " + ex.Message);
            }
        }
    }
}
